package com.hs.android.fortress.net;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.AsyncTask;
import android.util.Log;

public class AsyncNetworking extends AsyncTask<Void, Void, String>{
	
	private static final String TAG = AsyncNetworking.class.getSimpleName();
	
	public static final String MIME_TEXT = "text/plain";	
	public static final String MIME_JSON = "application/json";
	public static final String MIME_FORM = "application/x-www-form-urlencoded";
	
	private Context mContext;
	private ProgressDialog progDailog;
	private onNetworkProcessListener mNetListener;
	private NetworkCommand mCommand;
	
	private AsyncNetworking(Context context, NetworkCommand command, onNetworkProcessListener netListener) {
		mContext = context;
		mNetListener = netListener;
		mCommand = command;
	}
	
	public static AsyncNetworking newInstance(Context context, NetworkCommand command, onNetworkProcessListener netListener){
		AsyncNetworking asnc = new AsyncNetworking(context, command, netListener);
		
		return asnc;
	}
	
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		if(mContext instanceof Activity){
			progDailog = new ProgressDialog(mContext);
	        progDailog.setMessage("Loading...");
	        progDailog.setIndeterminate(false);
	        progDailog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
	        progDailog.setCancelable(true);
	        progDailog.show();
		}
	}
	
	@Override
	protected String doInBackground(Void... params) {
		String result = null;
		
		try {
			HttpResponse response = mCommand.callMethod();
			
			// status 200 means "OK" - the request has succeeded
			if(response.getStatusLine().getStatusCode() != 200) {
				Log.d(TAG,"Status: "+response.getStatusLine().getStatusCode());
				return null;
			}
			
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
		
			
			Log.d(TAG,"Status: "+response.getStatusLine().getStatusCode() );
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	protected void onPostExecute(String result) {
		Log.d(TAG,"result : " + result);
		
		if(mContext instanceof Activity){
			try{
				progDailog.dismiss();
			}catch(Exception e){}
		}
		
		if(result == null) {
			Log.d(TAG,"Something went wrong because the result was null.");
		}
		
		if(mNetListener != null)
			mNetListener.onPostExecute(result);
	}
	
	public interface onNetworkProcessListener {
		void onPostExecute(String result);	
	}
	
	
	public static Builder makeNetworkProblemDialog(Activity activity){
		return new AlertDialog.Builder(activity)
		.setTitle("Network Error")
		.setMessage("Network/Server Problem")
		.setCancelable(false);
	}
	
}
